/*
 * Copyright (c) 2009 by unreserved.org.
 * All rights reserved.
 */

package org.unreserved.ladderplot;

public class Message implements Event, HAnchor {
    protected Player from;
    protected Player to;
    protected Label label;
    
    protected String details;
    
    /** Pattern used for drawing line. */
    protected String linePattern = "-";
    
    /** Left-to-right line ending. */
    protected String ltrLineEnding = ">";
    
    /** Right-to-left line ending. */
    protected String rtlLineEnding = "<";
    
    public Message(Player from, Player to, String text) {
        this.from = from;
        this.to = to;
        
        label = new Label(text, this);
        label.setAnchorPos(XPosition.LEFT);
        label.setLabelPos(XPosition.LEFT);
        label.setTextAlignment(Alignment.LEFT);
    }
    
    public Player getFrom() {
        return from;
    }
    
    public Player getTo() {
        return to;
    }
    
    public Player getLeftPlayer() {
        return (isLTR() ? from : to);
    }
    
    public Player getRightPlayer() {
        return (isLTR() ? to : from);
    }
    
    public Label getLabel() {
        return label;
    }
    
    public void setDetails(String details) {
        this.details= details;
    }
    
    public String getDetails() {
        return details;
    }
    
    public void setLinePattern(String s) {
        linePattern = s;
    }
    
    public String getLinePattern() {
        return linePattern;
    }
    
    public void setLtrLineEnding(String s) {
        if (s == null) s = "";
        ltrLineEnding = s;
    }
    
    public void setRtlLineEnding(String s) {
        if (s == null) s = "";
        rtlLineEnding = s;
    }
    
    public String getLineEnding() {
        return isLTR() ? ltrLineEnding : rtlLineEnding;
    }
    
    public boolean isLTR() {
        return from.getColumn() < to.getColumn();
    }
    
    // from Event
    public void accept(ModelVisitor visitor) {
        visitor.visit(this);
    }
    
    // from HAnchor interface
    public int getStart() {
        return getLeftPlayer().getStart() + 1;
    }
    
    // from HAnchor interface
    public int getWidth() {
        return Math.abs(from.getStart() - to.getStart() - 1);
    }
    
    public String toString() {
        return "Message[" + from + "," + to + "," + label.getText() + "]";
    }
}
